#ifndef __fmc_flash_H
#define __fmc_flash_H


#ifdef __cplusplus
extern "C" {
#endif 

#include "gd32e10x.h"
#include "gd32e10x_fmc.h"

//gd32的起始地址
#define		FMC_START_MCU_ADR		((uint32_t)0x08000000U) 

//可擦除块字节数
#define		FMC_PAGE_SIZE       ((uint16_t)0x400U)

//page0~19(20k)存储iap，page20存升级标记，page21~99(78k)存app，page100~127(28k)存应用数据
#define		FMC_APP_START_PAGE		21
#define		FMC_APP_END_PAGE	    99		//21-99共78k用于存程序

#define		FMC_APP_START_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * FMC_APP_START_PAGE) //0x08004000  //IAP 21k
#define		FMC_APP_END_ADR	    (FMC_START_MCU_ADR + FMC_PAGE_SIZE * (FMC_APP_END_PAGE+1)) //实际到0x08019000-1



//把IAP升级标记存储到page15了，一共1k，只存一个标记0xAABBCCDD代表需要升级
#define		FMC_OTA_FlAG_PAGE		20
#define		FMC_OTA_FLAG_PAGE_ADR	    (FMC_START_MCU_ADR + FMC_PAGE_SIZE * (FMC_OTA_FlAG_PAGE))
#define		FMC_OTA_Flag				0xAABBCCDD

//最后的100~127共28k用于存储数据
#define		FMC_RS_ADR	(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 100)	//page100 预留

//page101 存继电器状态，存储格式，
//第1字节A2做标记，第2字节数据（1通电、2断电），第3字节为第2字节取反
//另外在512位置存个备份，格式相同
#define		FMC_RELAY_STA_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 101)	

//参数表
#define		FMC_PARA_TABLE_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 102)	
#define		FMC_PARA_TABLE2_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 103)	

//车身存储
#define		FMC_CARINF_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 104)	
#define		FMC_CARINF_ADR2		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 105)	

//SIM切卡
#define		FMC_SWSIM_STA_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 106)	

//测试程序需要保存的信息（dev_id,car_id,blemac,ccid_in,ccid_out）
#define		FMC_PRI_INFO_ADR		(FMC_START_MCU_ADR + FMC_PAGE_SIZE * 107)

/*车辆安全密钥数据存储*/
#define     FMC_KEY_INFO_ADR        (FMC_START_MCU_ADR + FMC_PAGE_SIZE * 108)

uint8_t get_chk_xorForFlash(uint8_t * da, uint16_t len);
uint8_t write_fmc_flash(uint32_t adr, uint8_t * pdata , uint32_t len);
uint8_t read_fmc_flash(uint32_t adr, uint8_t * pdata , uint32_t len);
void erase_fmc_flash(uint32_t start_adr, uint32_t page_cnt);
uint8_t chk_fmc_UpFlag(void);
void eraseGD_fmc_UpFlag(void);
uint8_t writeGD_fmc_UpFlag(uint32_t fileSize);

uint8_t savePara1024WithAdr(uint32_t adr, uint8_t *para, uint16_t len);
uint8_t readPara1024WithAdr(uint32_t adr, uint8_t * pPara, uint16_t len);



#ifdef __cplusplus
}
#endif


#endif



